// pages/commentEdit/commentEdit.js
import * as API_Order from '../../api/order'
import * as API_Store from '../../api/store'
import * as API_Comment from '../../api/comment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commit:true,
    comment:{
      orderId:'',//订单Id
      commentContent:'',//评价内容
      commentImages:'',//评价图片
      commentVideo:'',//评价视频
      storeLevel:'',//综合星级
      effectLevel:'',//效果星级
      environmentLevel:'',//环境星级
      serviceLevel:''//服务星级
    },
    order:{},
    store:{},
    startList:[,,,,,]
  },

  save(){
    let flag = !this.data.commit
    this.setData({commit:flag})
    let comment = this.data.comment
    if(!comment.effectLevel.trim()){
      wx.showToast({title: '请为效果打分',icon:'none',duration:1500})
      this.setData({commit:!flag})
    }else if(!comment.environmentLevel.trim()){
      wx.showToast({title: '请为环境打分',icon:'none',duration:1500})
      this.setData({commit:!flag})
    }else if(!comment.serviceLevel.trim()){
      wx.showToast({title: '请为服务打分',icon:'none',duration:1500})
      this.setData({commit:!flag})
    }else if(!comment.commentContent.trim()){
      wx.showToast({title: '请输入评价内容',icon:'none',duration:1500})
     this.setData({commit:!flag})
    }else{
      API_Comment.create(comment).then(res=>{
        if(res.code === 500) {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.setData({
            commit: true
          })
          return
        }
        var pages = getCurrentPages();
        var beforePage = pages[pages.length - 2];
        wx.showToast({
          title: '提交成功',
          success(res){
            setTimeout(function () {
              wx.navigateBack({
                delta: 0,
                success(){
                  beforePage.flush()
                }
              })  
          },1500)
          }
        })
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'comment.orderId':options.orderId
    })
    API_Order.getOrder(options.orderId).then(res=>{
      console.log(res)
      API_Store.getInfo(res.data.storeId).then(res=>{
        this.setData({
          store:res.data
        })
      })
      this.setData({
        order:res.data
      })
    })
  },
  selectServiceLevel:function(e){
    let level = e.currentTarget.dataset.index;
    this.setData({
      serviceLevel:level.toFixed(1),
      'comment.serviceLevel':level+'.0'
    });
  },
  selectEnvironmentLevel(e){
    let level = e.currentTarget.dataset.index;
    this.setData({
      environmentLevel:level.toFixed(1),
      'comment.environmentLevel':level+'.0'
    });
  },
  selectEffectLevel:function(e){
    let level = e.currentTarget.dataset.index;
    this.setData({
      effectLevel:level.toFixed(1),
      'comment.effectLevel':level+'.0'
    });
  },
  changeInput(e){
    this.setData({
      'comment.commentContent':e.detail.value
    })
  },
   //图片上传回调
   uploadImgs(e){
    var imgs = e.detail.picsList.join(",")
    this.setData({"comment.commentImages":imgs})
  },
    //视频上传回调
    uploadVideo(e){
      var videos = e.detail.videos.join(",")
      this.setData({"comment.commentVideo":videos})
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})